﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;

using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
using ZF.WS.Admin;
using ZF.BLL.ServiceModel;
using ZF.Model;


namespace ZF.WS.Admin.service
{
    public class log : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.SysLog; }
            protected set
            {
                base.Module = value;
            }
        }
        public virtual string ListPage { get { return string.Format("{0}/site/logList.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("{0}/site/logManage.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "系统日志"; } }
    }
    public partial class logList : log
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {

        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.Sys_Log, bool>>> filterList = new List<Expression<Func<Model.Sys_Log, bool>>>();
                IQueryable<Model.Sys_Log> selector = dc.Sys_Log;

                foreach (var filter in filterList)
                    selector = selector.Where(filter);
                var selWhere = selector.Where(this.gvList); ;
                var selPage = selWhere.OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);
                this.gvList.RowCount = selWhere.Count();
                var source = selPage.ToList();

                this.gvList.DataSource = source;
                this.gvList.DataBind();

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {

                    var list = dc.Sys_Log.Where(p => idArr.Contains(p.LogId)).ToList();
                    dc.Sys_Log.DeleteAllOnSubmit(list);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "日志";
                if (isSuccess)
                    SiteInfo.WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(List<Sys_Log> list)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {

                    dc.Sys_Log.AttachAll(list);
                    dc.Sys_Log.DeleteAllOnSubmit(list);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "日志";
                if (isSuccess)
                    SiteInfo.WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;

            using (var dc = SiteInfo.CreateDBDataContext())
            {
                switch (bc.CommandArgument)
                {
                    case "select":

                        var idArrTemp = this.gvList.GetCheckKeys();
                        if (idArrTemp.Length == 0)
                            throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                        var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                        Delete(idArr);

                        break;

                    case "lastMonth": // 上月


                        DateTime lastMonthTime = DateTime.Now.AddMonths(-1);

                        var list = dc.Sys_Log.Where(l => l.CreateTime < lastMonthTime).ToList();

                        Delete(list);

                        break;

                    case "halfYear":  // 半年

                        TimeSpan t2 = new TimeSpan(180, 0, 0, 0); // 半年以前
                        DateTime halfYearTime = DateTime.Now - t2;

                        var list1 = dc.Sys_Log.Where(l => l.CreateTime < halfYearTime).ToList();

                        Delete(list1);

                        break;

                    default:
                        Delete(bc.CommandArgument.Convert<int>());
                        break;
                }
            }

        }

        public void Search(object sender, EventArgs e)
        {
            //this.gvList.RemoveAllFilterExpression();
            //if (this.tbTitle.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.Title.Contains(@0)", this.tbTitle.Text);
            //if (this.tbHouseNo.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.HouseId=@0", this.tbHouseNo.Text.Convert<int>()-10000);
            //if (this.ddlHouseType.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.HouseType=@0", this.ddlHouseType.SelectedValue);
            //if (this.ddlHouseStatus.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.Status=@0", this.ddlHouseStatus.SelectedValue.Convert<int>());
            //if (this.tbEndTime1.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            //if (this.tbEndTime2.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());

            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }

    }
  
}
